package org.example.springcloud.config;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.example.springcloud.listener.ServiceListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class NacosConfig {

    @Autowired
    private ServiceListener listener;

    @Bean
    public void addListener() throws NacosException {
        String dataId = "usermodule";

        String group = "DEFAULT_GROUP";

        Properties properties = new Properties();
        properties.put("serverAddr", "127.0.0.1:8848");
        properties.put("namespace","84c5dbda-4eb8-46fb-a525-6d8503d546b4");
        //创建NacosConfigService的实例
        ConfigService configService = NacosFactory.createConfigService(properties);
        //添加监听器，监听配置的变更
        configService.addListener(dataId, group, listener);
    }


}
